package cn.edu.sasu.service.impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.common.util.DateUtils;
import cn.edu.sasu.domain.dto.SysRoleSaveDTO;
import cn.edu.sasu.domain.entity.SysRole;
import cn.edu.sasu.domain.vo.SysRoleVO;
import cn.edu.sasu.mapper.SysRoleMapper;
import cn.edu.sasu.service.SysRoleService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 功能：
 * 作者：邓鑫
 * 日期：2024/4/26 19:11
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public LayuiRespResult findSysRoleAll(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysRoleVO> sysRoles = sysRoleMapper.findSysRoleList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = sysRoleMapper.countSysRole(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize,sysRoles);
    }


    @Override
    public RespResult delSysRoleByRoleId(Integer roleId) {
        sysRoleMapper.updateSysRoleDeleteFlagByRoleId(roleId,Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult batchdel(Integer[] roleIds) {
        sysRoleMapper.updateSysRoleDeleteFlagByRoleIds(Arrays.asList(roleIds),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public LayuiRespResult search(Integer page, Integer limit, String username) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysRoleVO> sysRoles = sysRoleMapper.searchSysRoleList(Constant.DELETE_FLAG_YES,offset,rows,username);
        Integer totalSize = sysRoleMapper.countSearchSysRole(Constant.DELETE_FLAG_YES,username);
        return LayuiRespResult.success(totalSize,sysRoles);
    }

    @Override
    public RespResult save(SysRoleSaveDTO sysRoleSaveDTO) {
        SysRole user = sysRoleMapper.findSysRoleByUsername(sysRoleSaveDTO.getRoleName());
        if(user != null){
            return RespResult.error("该角色已经存在，请重新输入！");
        }
        // 对象拷贝
        SysRole sysRole = BeanUtil.copyProperties(sysRoleSaveDTO,SysRole.class);
        sysRole.setCreateDate(DateUtils.getCurrentDate());
        sysRole.setCreateDate(new Date());
        sysRole.setUpdateDate(new Date());
        sysRole.setDeleteFlag(Constant.DELETE_FLAG_NO);
        sysRoleMapper.save(sysRole);
        return RespResult.success("添加成功");
    }

    @Override
    public RespResult updateInfo(SysRoleVO roleVO) {
        roleVO.setUpdateDate(DateUtils.getCurrentDate());
        Integer integer = sysRoleMapper.updateInfo(roleVO);
        if (integer > 0) {
            return RespResult.success("修改成功", sysRoleMapper.findSysRolerByRoleId(roleVO.getRoleId()));
        } else {
            return RespResult.error("修改失败");
        }
    }
}
